/**
 * 
 */
package sailing.ui.systemsetting.model;

import java.util.List;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.BankName;
import sailing.listener.DataListener;
import sailing.managers.BankNameManager;
import sailing.ui.model.SailingTableModel;
import cjlite.utils.Lists;

/**
 * @author ming
 * 
 */
public class BankNameTableModel extends SailingTableModel implements DataListener {

	private String[] columnName = { "银行名称", "操作" };
	private List<BankName> bnList = Lists.newArrayList();
	private boolean showEditBtn = false;


	/**
	 * @param application
	 */
	public BankNameTableModel(SingleFrameApplication application) {
		super(application);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return this.bnList.size();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return showEditBtn ? columnName.length : columnName.length - 1;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= this.bnList.size()) {
			return "";
		}

		BankName bn = this.bnList.get(rowIndex);

		switch (columnIndex) {
		case 0:
			return bn.getName();
		case 1:
//			return Utils.dateFormat(bn.getCreateTime());
//		case 2:
			return bn;
		}

		return "";
	}


	@Override
	public String getColumnName(int column) {
		return columnName[column];
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.listener.DataListener#dataChanged()
	 */
	@Override
	public void dataChanged() {
		updateModel();
		super.fireTableDataChanged();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.model.SailingTableModel#updateModel()
	 */
	@Override
	public void updateModel() {
		BankNameManager bnm = this.getApplication().getContext().getIoc().getInstance(BankNameManager.class);
		bnList.clear();
		bnList.addAll(bnm.getAllBankNameList());
	}


	public void showEditBtn(boolean selected) {
		this.showEditBtn = selected;
	}


	/* (non-Javadoc)
	 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex==this.columnName.length-1;
	}
	
	

}
